<?php

class CartsController extends Controller
{
		//public $backend_left_menu = "_left_menu";
		public $backend_left_menu = "application.modules.products.views.admin_menu._left_menu";
		public $backend_left_menu_datas = array('list'	=>	array());
		//public $backend_left_menu_datas = false;
		private $_model;
		public $layout='//layouts/main';
		public $pageTitle = "Manage carts ";
		public function init()
		{
				Yii::app()->theme = 'backend';
				parent::init();
		}

	/**
	 * @todo Kiểm tra quyền
	 */
	
	/**
	 * @return string Trả về các action (cách nhau bằng dấu phẩy) cho phép truy cập mà không cần xác thực quyền
	public function allowedActions() {
		return '*';
	}
	 */
	
	public function actionView($id)
	{
		$this->pageTitle = 'View a cart';
		
		$configCart = Configs::model()->findAll();
		
		$this->render('view', array(
			'model' => $this->loadModel($id),
			'configCart' => $configCart
		));
	}
	
	public function actionCreate()
	{
		
		$model = new Carts;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		if (isset($_POST['Carts'])) {
			$model->attributes = $_POST['Carts'];
		   
			
			if ($model->save()) {
				$this->redirect(array('admin'));
			}
		}

		$this->render('create', array(
			'model' => $model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$this->pageTitle = 'Create a category';
		$model = $this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['Carts'])) {
			$model->attributes = $_POST['Carts'];
			
			// Upload image
		   
			
			if ($model->save())
				$this->redirect(array('admin'));
		}

		$this->render('update', array(
			'model' => $model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
	   	if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$model = $this->loadModel($id);
		   
			$model->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			//if (!isset($_GET['ajax']))
				//$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	  	}
	  	else
			throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model = new Carts('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['Carts']))
			$model->attributes = $_GET['Carts'];

		$this->render('admin', array(
			'model' => $model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model = Carts::model()->findByPk($id);
		if ($model === null)
			throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'product-categories-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * This method is using to update config_id for cart
	 * @param unknown_type $id
	 * @param unknown_type $config_id
	 */
	public function actionUpdateConfig($id = NULL, $config_id = NULL) {
		if (isset($id) && isset($config_id)) {
			$cartModel = Carts::model()->findByPk($id);
			$configModel = Configs::model()->findByPk($config_id);
			
			if (!empty($cartModel) && !empty($configModel)) {
				$cartModel->config_id = $config_id;
				$cartModel->save();
			}
		}
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
}
